# -*- coding: utf-8 -*-

from odoo import models, fields, api, _
from odoo.tools import float_compare


# class ZfySaleOrderLine(models.Model):
#     _inherit = 'sale.order.line'


    # def _action_procurement_create(self):
    #     """
    #     Create procurements based on quantity ordered. If the quantity is increased, new
    #     procurements are created. If the quantity is decreased, no automated action is taken.
    #     """
        # precision = self.env['decimal.precision'].precision_get('Product Unit of Measure')
        # new_procs = self.env['procurement.order']  # Empty recordset
        # for line in self:
        #     if line.state != 'sale' or not line.product_id._need_procurement():
        #         continue
        #     qty = 0.0
        #     for proc in line.procurement_ids:
        #         qty += proc.product_qty
        #     if float_compare(qty, line.product_uom_qty, precision_digits=precision) >= 0:
        #         continue
        #
        #     if not line.order_id.procurement_group_id:
        #         vals = line.order_id._prepare_procurement_group()
        #         line.order_id.procurement_group_id = self.env["procurement.group"].create(vals)
        #
        #     vals = line._prepare_order_line_procurement(group_id=line.order_id.procurement_group_id.id)
        #     vals['product_qty'] = line.product_uom_qty - qty
        #     vals['remark'] = line.order_id.remark
        #     new_proc = self.env["procurement.order"].with_context(procurement_autorun_defer=True).create(vals)
        #     new_proc.message_post_with_view('mail.message_origin_link',
        #         values={'self': new_proc, 'origin': line.order_id},
        #         subtype_id=self.env.ref('mail.mt_note').id)
        #     new_procs += new_proc
        # new_procs.run()
        # return new_procs
        #modify
        # abg = super(ZfySaleOrderLine,self)._action_procurement_create()
        # abg.remark = self.order_id.remark
        # return abg

class ZfySaleOrder(models.Model):
    _inherit = 'sale.order'

    @api.multi
    def action_confirm(self):
        res = super(ZfySaleOrder, self).action_confirm()
        procurement_ids_temp = self.procurement_group_id.procurement_ids
        temp_line_remark = 1
        temp_line_track_number = 1
        sale_re=[]
        sale_tr=[]
        sale__re_dict = {}
        sale__tr_dict = {}
        # for pru_order in self.env['procurement.order'].browse(procurement_ids_temp):
        lg = len(procurement_ids_temp) / 2
        for index,pro_id_temp in enumerate(procurement_ids_temp):
            if pro_id_temp.sale_line_id:
                temp_line_remark = pro_id_temp.sale_line_id.line_remark
                temp_line_track_number = pro_id_temp.sale_line_id.track_number
                dict_key = str(pro_id_temp.product_id.id)+str(pro_id_temp.product_qty)+'-'+str(pro_id_temp.id)
                print dict_key
                dict_re = pro_id_temp.sale_line_id.line_remark
                dict_tr = pro_id_temp.sale_line_id.track_number
                sale__re_dict[dict_key] = dict_re
                print sale__re_dict
                sale__tr_dict[dict_key] = dict_tr
                sale_re.append(temp_line_remark)
                sale_tr.append(temp_line_track_number)
            else:
                if pro_id_temp.purchase_line_id:
                    pli = str(pro_id_temp.product_id.id)+str(pro_id_temp.product_qty)
                    pro_id_temp.purchase_line_id.order_id.remark = self.remark
                    # pro_id_temp.purchase_line_id.line_remark = sale_re[index-lg]
                    # pro_id_temp.purchase_line_id.track_number = sale_tr[index-lg]
                    for key,value in sale__re_dict.items():
                        arr = key.split('-')
                        if arr[0] == pli:
                            pro_id_temp.purchase_line_id.line_remark = value
                            pro_id_temp.purchase_line_id.track_number = sale__tr_dict[key]
                            del sale__re_dict[key]
                            del sale__tr_dict[key]
                            break
            # self.env['procurement.order'].filtered('')
        sp = self.env['stock.picking'].search([('sale_id', '=', self.id)])
        if sp and self.project_id:
            for spking in sp:
                spking.project_id = self.project_id
        # return res
        return True